<template>
  <div class="tktk_container">
    <!--菜单表格-->
    <div class="search_box">
      <el-form :inline="true" :model="params" class="demo-form-inline">
        <el-form-item label="商品名称">
          <el-input v-model="params.param" size="mini" placeholder="请输入商品名称"></el-input>
        </el-form-item>
        <el-form-item label="商品分类">
          <el-select v-model="params.type" placeholder="请选择" size="mini">
            <el-option label="全部" value=""></el-option>
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="onSubmit" size="mini" icon="el-icon-search">查询</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="tktk_btn"><el-button type="primary" size="mini" @click="handleAdd" icon="el-icon-plus">新增商品</el-button>
    </div>
    <el-table v-loading="loading" :data="goodsList" row-key="id">
      <el-table-column prop="id" fixed="left" width="180" label="商品编号" align="center"></el-table-column>
      <el-table-column prop="name" label="商品名称" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column label="商品图片" :show-overflow-tooltip="true" align="center">
        <template slot-scope="scope">
          <template v-if="scope.row.fileList.length > 0">
            <el-image style="height: 60px; border-radius: 5px;" :src="scope.row.fileList[0].fileUrl"
              :preview-src-list="getPreviewList(scope.row.fileList)"></el-image>
          </template>
        </template>
      </el-table-column>
      <el-table-column prop="price" label="到手价（¥）" :show-overflow-tooltip="true" align="center"></el-table-column>
      <el-table-column prop="brokerageRatio" label="佣金比（%）" align="center"></el-table-column>
      <el-table-column prop="brokerage" label="佣金（¥）" align="center"></el-table-column>
      <el-table-column prop="readCount" label="浏览量" align="center"></el-table-column>
      <el-table-column prop="clickCount" label="点击量" align="center"></el-table-column>
      <el-table-column prop="shopName" label="所属门店" align="center"></el-table-column>
      <el-table-column prop="status" label="状态" align="center">
        <template slot-scope="scope">
          <el-tag class="status_tag" v-if="scope.row.status == '0'" type="success">正常</el-tag>
          <el-tag class="status_tag" v-else type="info">已下架</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="创建时间" width="180" align="center" prop="createTime">
        <template slot-scope="scope">
          <span>{{ scope.row.createTime }}</span>
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" fixed="right" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope)">修改</el-button>
          <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="box-center">
      <el-pagination @current-change="handleCurrentChange" :current-page.sync="params.pageNum"
        layout="prev, pager, next" :page-size="params.pageSize" :total="total">
      </el-pagination>
    </div>

    <!--新增编辑表格-->
    <el-drawer :title="title" :visible.sync="drawer" :direction="direction" :before-close="cancel" size="40%">
      <el-form ref="form" :model="form" :rules="rules" label-width="100px" size="small">
        <el-form-item label="商品名称" prop="name">
          <el-input v-model="form.name" placeholder="商品名称" maxlength="30" class="w260" />
        </el-form-item>
        <el-form-item label="商品分类" prop="type">
          <el-select v-model="form.type" placeholder="请选择" class="w260">
            <el-option v-for="item in options" :key="item.id" :label="item.name" :value="item.id">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="商品图片" prop="fileList">
          <div class="upload-goods-accept">建议上传尺寸: 227 * 230</div>
          <div class="upload-goods">
            <el-upload :action="actionUrl" list-type="picture-card" ref="uploads" multiple :headers="headers"
              :file-list="form.fileList" :on-preview="handlePictureCardPreview" :on-remove="handleRemove"
              :on-change="changeUpdate">
              <i class="el-icon-plus"></i>
            </el-upload>
          </div>
        </el-form-item>
        <el-form-item label="商品价格" prop="price">
          <el-input v-model="form.price" placeholder="商品价格" maxlength="30" class="w260" />
        </el-form-item>
        <el-form-item label="所属门店" prop="shopName">
          <el-input v-model="form.shopName" placeholder="请输入门店名称" maxlength="30" class="w260" />
        </el-form-item>
        <el-form-item label="门店链接" prop="shopUrl">
          <el-input v-model="form.shopUrl" placeholder="请输入门店地址" maxlength="30" class="w260" />
        </el-form-item>
        <el-form-item label="佣金比" prop="brokerageRatio">
          <el-input-number placeholder="佣金比" v-model="form.brokerageRatio" :min="0" :max="100"
            class="w260"></el-input-number> %
        </el-form-item>
        <el-form-item label="佣金" prop="brokerage">
          <el-input-number v-model="form.brokerage" :min="0" class="w260"></el-input-number>
        </el-form-item>
        <el-form-item label="点击量" prop="clickCount">
          <el-input-number v-model="form.clickCount" :min="0" class="w260"></el-input-number>
        </el-form-item>
        <el-form-item label="浏览量" prop="readCount">
          <el-input-number v-model="form.readCount" :min="0" class="w260"></el-input-number>
        </el-form-item>
        <el-form-item prop="status" label="商品状态">
          <div class="radio-group">
            <label><input value="0" v-model="form.status" type="radio" name="status" />正常</label>
            <label><input value="1" v-model="form.status" type="radio" name="status" />下架</label>
          </div>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" @click="submitForm('form')">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </el-form-item>
      </el-form>
    </el-drawer>

    <!--照片预览-->
    <el-dialog :visible.sync="dialogVisible">
      <img width="100%" :src="dialogImageUrl" alt="">
    </el-dialog>
  </div>
</template>

<script>
import { goodsList, addGoods, deleteGoods, updateGoods } from '@/api/tktk/goods'
import { classifyList } from '@/api/tktk/classify'
export default {
  name: 'goodsList',
  data() {
    return {
      actionUrl: '/api/cargo/file/upload',
      loading: false,
      drawer: false,
      dialogVisible: false,
      title: '新增商品',
      dialogImageUrl: '',
      direction: 'rtl',
      total: 0,
      searchParams: {
        pageNum: 1,
        pageSize: -1
      },
      headers: {
        Authorization: this.$store.getters.token
      },
      goodsList: [],
      options: [],
      form: {
        id: '',
        name: '',
        price: '',
        brokerage: '',
        shopName: '',
        shopUrl: '',
        type: '',
        readCount: '',
        clickCount: '',
        brokerageRatio: '',
        status: '1',
        feature: '1,2',
        source: 'douyin',
        fileList: [],
      },
      rules: {
        name: [
          { required: true, message: '商品名称不能为空', trigger: 'blur' }
        ],
        price: [
          { required: true, message: '商品价格不能为空', trigger: 'blur' }
        ],
        brokerage: [
          { required: true, message: '商品佣金不能为空', trigger: 'blur' }
        ],
        brokerageRatio: [
          { required: true, message: '商品佣金比不能为空', trigger: 'blur' }
        ],
        type: [
          { required: true, message: '请选择商品分类', trigger: 'blur' }
        ],
        fileList: [
          { required: true, message: '请上传商品图片', trigger: 'blur' }
        ],
        shopUrl: [
          { required: true, message: '商品地址不能为空', trigger: 'blur' }
        ],
        shopName: [
          { required: true, message: '门店名称不能为空', trigger: 'blur' }
        ],
        status: [
          { required: true, message: '请选择商品状态', trigger: 'blur' }
        ]
      },
      params: {
        param: '',
        type: '',
        feature: -1,
        pageNum: 1,
        pageSize: 10,
      }
    }
  },
  methods: {
    handleUpdate(row) {
      const { id, name, price, brokerage, shopName, shopUrl, type, readCount, clickCount, brokerageRatio, status, feature, source, fileList } = row.row
      console.log(row);
      this.form = {
        id,
        name,
        price,
        brokerage,
        shopName,
        shopUrl,
        type,
        readCount,
        clickCount,
        brokerageRatio,
        status,
        feature,
        source,
        fileList: fileList.map(item => {
          return {
            name: item.fileName,
            fileName: item.fileName,
            fileUrl: item.fileUrl,
            url: item.fileUrl,
            id: item.id
          }
        })
      }
      this.title = '编辑商品'
      this.drawer = true
    },
    classifyList() {
      classifyList(this.searchParams).then(res => {
        if (res.code == 200) {
          this.options = res.rows || []
        }
      })
    },
    handleAdd() {
      this.drawer = true
      this.form.id = ''
      this.form.name = ''
      this.form.price = ''
      this.form.brokerage = ''
      this.form.shopName = ''
      this.form.shopUrl = ''
      this.form.type = ''
      this.form.readCount = ''
      this.form.clickCount = ''
      this.form.brokerageRatio = ''
      this.form.status = ''
      this.form.feature = '1,2'
      this.form.source = 'douyin'
      this.form.fileList = []
    },
    changeUpdate(file, fileList) {
      console.log(file, fileList);
      if (file.response) {
        if (file.response.code == 200) {
          this.form.fileList.push({
            name: file.response.originalFilename,
            fileName: file.response.originalFilename,
            fileUrl: file.response.url,
            url: file.response.url,
            uid: file.uid
          })
        } else {
          this.form.fileList = this.form.fileList.filter(item => item.uid !== file.uid);
          this.$message.error(file.response.msg)
          return false
        }
      }
    },
    getPreviewList(fileList) {
      let list = []
      fileList.map(item => {
        list.push(item.fileUrl)
      })
      return list
    },

    handleCurrentChange(page) {
      this.params.pageNum = page
      this.getGoodsList()
    },

    onSubmit() {
      this.params.pageNum = 1
      this.getGoodsList()
    },

    handleDelete(row) {
      const _this = this
      this.$confirm('确定要删除该商品吗?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        deleteGoods({ id: row.id }).then(res => {
          if (res.code === 200) {
            _this.$message.success('删除成功')
            _this.getGoodsList()
          } else {
            _this.$message.error(res.msg)
          }
        })
      })

    },
    submitForm(ruleForm) {
      this.$refs[ruleForm].validate((valid) => {
        if (valid) {
          if (this.form.id) {
            updateGoods(this.form).then(res => {
              if (res.code == 200) {
                this.$message.success('编辑成功')
                this.drawer = false
                this.getGoodsList()
              } else {
                this.$message.error(res.msg)
              }
            })
          } else {
            addGoods(this.form).then(res => {
              if (res.code == 200) {
                this.$message.success('添加成功')
                this.drawer = false
                this.getGoodsList()
              } else {
                this.$message.error(res.msg)
              }
            })
          }
        }
      })
    },
    cancel() {
      this.drawer = false
    },
    changeStatus(row, type) {
      if (type == 1) {
        // 上架
      } else {
        // 下架
      }
    },
    handleRemove(file) {
      if (file.uid) {
        this.form.fileList = this.form.fileList.filter(item => item.uid !== file.uid);
      } else {
        this.form.fileList = this.form.fileList.filter(item => item.id !== file.id);
      }
    },
    handlePictureCardPreview(file) {
      this.dialogImageUrl = file.url;
      this.dialogVisible = true;
    },
    // 获取商品列表
    getGoodsList() {
      goodsList(this.params).then(res => {
        if (res.code == 200) {
          this.goodsList = res.rows || []
          this.total = res.total || 0
        }
      })
    }
  },

  mounted() {
    this.classifyList()
    this.getGoodsList()
  }
}



</script>

<style lang="scss">
.search_box {
  margin-bottom: 20px;
}

.upload-goods {
  .el-upload--picture-card {
    width: 80px;
    height: 80px;
    line-height: 78px;

    i {
      font-size: 20px;
    }
  }

  .el-upload-list__item {
    width: 80px;
    height: 80px;
  }
}
</style>
